(function () {

var $modal = $('#pop-modal');

		//获取验证码
		$('.auth-btn').on('click',function () {

			var iPhone = $('input[name="phone"]').val();

			var v = new Validator();
    			
			if(!v.isPhone(iPhone))
			{
				$('.am-modal-bd').text(language.errorPhone);
					$modal.modal();
					setTimeout(function () {
						$modal.modal('close');
					},iWiating);
					return;
			}

			getJson(Domain+'/settled/doSendVerifyCode.do', {
					mobile:iPhone,
					kaptcha:'XXXXXXX'
			}, function (res) {
			  
			  if(!res.success){
		
			    $('.am-modal-bd').text(res.errorMessage);
					$modal.modal();
					setTimeout(function () {
						$modal.modal('close');
					},iWiating);

			  }
			  else{

					oMenu = res.data.widgetData;
			  }
			});

		});

		//下一步
		$('#next').on('click',function () {

			var iJidi = $('select[name="jidi"]').val();
			var iPhone = $.trim($('input[name="phone"]').val());
			var sName = $.trim($('input[name="name"]').val());
			var iCoder = $.trim($('input[name="coder"]').val());
			var sEmail = $.trim($('input[name="email"]').val());

			var v = new Validator();

			oEmptyJson = {
				bPhone:v.required(iPhone),
				bName:v.required(sName),
				bCoder:v.required(iCoder),
				bEmail:v.required(sEmail)
			};

			for (var arr in oEmptyJson)
			{
				if(!oEmptyJson[arr])
				{
					$('.am-modal-bd').text(language.fillAll);
					$modal.modal();
					setTimeout(function () {
						$modal.modal('close');
					},iWiating);
					return;
				}
			}

			//验证手机短信
			$.post(Domain+'/settled/doCheckVerifyCode.do', {
				mobile:iPhone,
				code:iCoder
			}, function(res,textStatus, xhr) {
				
				if(!res.success){
	
				    $('.am-modal-bd').text(res.errorMessage);
						$modal.modal();
						setTimeout(function () {
							$modal.modal('close');
						},iWiating);
						return;
				  }
				  else
				  {
				  		cookie.set('firstPInfo',JSON.stringify({ //保存信息近cookie
							jidiId:iJidi,
							phone:iPhone,
							name:sName,
							email:sEmail
						}),iCookieDays);

						alert('保存SUC');

				  }

			});


		});

	//读取交易基地列表
	if($('#jidilist').length)
	{
		getJidiList ({});
	}
	
	function getJidiList (Filter)
	 {
	 	var Filter = Filter;
	 		Filter.page = page.page;
	 		Filter.pageSize = 9999;

	 	getJson(Domain+'/ec/usedTradingBase/doSearchTradingBaseList.do', Filter, function (res) {

		  if(!res.success){
	
		    $('.am-modal-bd').text(res.errorMessage);
				$modal.modal();
				setTimeout(function () {
					$modal.modal('close');
				},iWiating);

		  }
		  else{

		     $('#jidilist').empty();

			 renderTmp('#jidilist', 'jidilisttpl',res);
			 	
		  }
		},function (error) {

			$('.am-modal-bd').text(language.ajaxFailed);
							$modal.modal();
						setTimeout(function () {
							$modal.modal('close');
						},iWiating);

		})
	 }

	   //获取层级

	   if($('#provincelist').length)
	   {
	   	 laodAreas ({});
	   }
	 
	 function laodAreas (Filter)
	 {
	 	getJson(Domain+'/common/doLoadAllArea.do', Filter, function (res) {

		  if(!res.success){
	
		    $('.am-modal-bd').text(res.errorMessage);
				$modal.modal();
				setTimeout(function () {
					$modal.modal('close');
				},iWiating);

		  }
		  else{

		  	console.log(res);

		  	for (var i = 0; i< res.data.length; i++)
		  	{
		  		$('#provincelist').append('<option value="'+res.data[i].value+'">'+res.data[i].text+'</option>');
		  	}

		  	oMenuData = res.data;

	  	 	 //createCityList(0,oMenuData);

		  }
		},function (error) {

			$('.am-modal-bd').text(language.ajaxFailed);
							$modal.modal();
						setTimeout(function () {
							$modal.modal('close');
						},iWiating);

		});
	 }

	//生成对应城市层级
	function createCityList(index,data)
	{
		$('#citylist').empty().append('<option value="" selected>市</option>');

			for (var i = 0 ; i < data[index].children.length; i++)
		  	{
		  		$('#citylist').append('<option value="'+data[index].children[i].value+'">'+data[index].children[i].text+'</option>');
		  	}

		 	createAreaList(data[index].children[0]);
	}

	//生成对应区域数据
	function createAreaList(data)
	{
		$('#arealist').empty().append('<option value="" selected>县</option>');

		for (var i = 0 ; i < data.children.length; i++)
	  	{
	  		$('#arealist').append('<option value="'+data.children[i].value+'">'+data.children[i].text+'</option>');
	  	}
	}


	//省份市级联动
	$('#provincelist').on('change',function () {

		iProvinceIndex = $(this).find('option').index($(this).find('option:selected'));
		iProvinceIndex = parseInt(iProvinceIndex)-1;

		if(iProvinceIndex==-1)
		{
			$('#citylist').empty().append('<option value="" selected>市</option>');
			$('#arealist').empty().append('<option value="" selected>县</option>');
			return;
		}

		console.log('change');
		createCityList(iProvinceIndex,oMenuData);

	});

	//市级区域联动
	$('#citylist').on('change',function () {

		iCityIndex = $(this).find('option').index($(this).find('option:selected'));
		iCityIndex = parseInt(iCityIndex)-1;

		if(iCityIndex==-1)
		{
			$('#arealist').empty().append('<option value="" selected>县</option>');
			return;
		}

		createAreaList(oMenuData[iProvinceIndex].children[iCityIndex]);

	});

	//区域查询
	$('#arealist').on('change',function () {

		iAreaIndex = $(this).find('option').index($(this).find('option:selected'));
		iAreaIndex = parseInt(iAreaIndex)-1;

		if(iAreaIndex==-1)
		{
			return;
		}

	});

	 //第二页表单
	 $('#secondnext').on('click',function () {


	 		var sShangjia = $.trim($('input[name="shangjiamc"]').val());
			var iSheng = $.trim($('select[name="sheng"]').val());
			var iShi = $.trim($('select[name="shi"]').val());
			var iXian = $.trim($('select[name="xian"]').val());
			var sXiangxi = $.trim($('input[name="xiangxidz"]').val());
			var sZhuying = $.trim($('textarea[name="zhuyingcp"]').val());

				//console.log(sShangjia+'||'+iSheng+'||'+iShi+'||'+iXian+'||'+sXiangxi+'||'+sZhuying);

			var iAreaId = null;

				if(iSheng)
				{
					iAreaId = iSheng;
				}

				if(iShi)
				{
					iAreaId = iShi;
				}

				if(iXian)
				{
					iAreaId = iXian;
				}

			var v = new Validator();

			oEmptyJson = {
				bShangjia:v.required(sShangjia),
				bSheng:v.required(iSheng),
				bXiangxi:v.required(sXiangxi),
				bZhuying:v.required(sZhuying)
			};

			for (var arr in oEmptyJson)
			{
				if(!oEmptyJson[arr])
				{
					$('.am-modal-bd').text(language.fillAll);
					$modal.modal();
					setTimeout(function () {
						$modal.modal('close');
					},iWiating);
					return;
				}
			}


			cookie.set('secondPInfo',JSON.stringify({ //保存信息近cookie
				shangjiamc:sShangjia,
				yingyeszd:iAreaId,
				xiangxidz:sXiangxi,
				zhuyingcp:sZhuying
			}),iCookieDays);

			alert('保存SUC2');


	 });

})();